﻿using UnityEngine;
using System.IO;

namespace MyAssetBundles {
    public class ContrastFolderProgress : FolderProgress {
        protected override void ShowProgress ()
        {
            string info = string.Format("process number: {0}, total number: {1}, progress: {2}%", target.processNum, target.totalNum, target.progress * 100);
            ShowProgress("Contrast", info);
        }
    }

    public class ContrastFolder : BaseFolder, IFolder {
        public void Generate (string path, IFolderProgress progress)
        {
            MyAssetBundlesVersions version = new MyAssetBundlesVersions(Path.GetDirectoryName(path));
            version.LoadLocalPath(path);
            if (version.Count == 0) return;

            SetProgress(progress, version.Count);
            int process = 0;
            foreach (var ver in version)
            {
                SetProcessNum(process++);
                if (ver.updateMode != _updateMode) continue;
                DecompressFolder decompress = FolderMgr.Create<DecompressFolder>();
                decompress._inTotalSize = ver.compressSize;
                decompress._outTotalSize = ver.decompressSize;
                decompress._inPath = ver.compressPackage;
                decompress.Generate(ver.decompressPackage, FolderProgress.Create<DecompressFolderProgress>());

                AnalysisFolder analysis = FolderMgr.Create<AnalysisFolder>();
                analysis.Generate(ver.decompressPackage, null);
                foreach (var item in analysis)
                {
                    MyFileInfo fileInfo = GetValue(item.Key);
                    if (fileInfo == null) continue;
                    Debug.Log("file name: " + item.Value.Name + " - " + fileInfo.Name);
                    Debug.Log("MD5: " + item.Value.MD5String + " - " + fileInfo.MD5String);
                    fileInfo.IsContrast = item.Value.MD5String.CompareTo(fileInfo.MD5String) != 0;
                }
            }
        }

        // 获得差异数量
        public int GetContrastNumber ()
        {
            int num = 0, resNum = 0;
            foreach(var item in this)
            {
                if (item.Value.IsContrast)
                {
                    num++;
                    if (!(MyAssetBundlesUtil.ExtensionEquals(item.Value.Name, "") || MyAssetBundlesUtil.IsManifestFile(item.Value.Name)))
                        resNum++;
                }
            }
            Debug.Log("contrast num: " + num + ",   res num: " + resNum);
            if (resNum == 0) return resNum;
            return num;
        }
    }
}


